<template>
  <el-dialog
    v-el-drag-dialog
    :title="title"
    :visible.sync="visible"
    width="500px"
    @close="reset"
  >
    <el-form ref="form" :rules="rules" class="form" :model="query" label-width="95px">
      <el-form-item label="科目类型：" class="merge-item" prop="subjectFlag">
        <el-select v-model="query.subjectFlag" :disabled="type==='edit'" style="width: 100%;" clearable>
          <el-option label="收入" :value="1" />
          <el-option label="支出" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="科目名称：" class="merge-item" prop="subjectName">
        <el-input v-model.trim="query.subjectName" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="备注：" class="merge-item">
        <el-input v-model="query.remark" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="显示顺序：" class="merge-item">
        <el-input v-model.trim="query.showIndex" placeholder="请输入" clearable />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="reset">
        关闭
      </el-button>
      <el-button :loading="$loadingPlugin.success " type="primary" @click="success">
        确认
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { accountSubjectSave } from '@/api'
  export default {
    name: 'CapitalSubject',
    components: {

    },
    data() {
      return {
        visible: false,
        title: null,
        type: 'add',
        rules: {
          subjectName: [{ required: true, message: '请输入', trigger: 'change' }],
          subjectFlag: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        query: {
          subjectCode: null,
          showIndex: null,
          remark: null,
          subjectName: null,
          subjectFlag: null
        }
      }
    },
    methods: {
      open(param) {
        this.visible = true
        this.type = 'add'
        this.title = '新增资金科目'
        if (param) {
          this.type = 'edit'
          this.title = `编辑科目资金：${param.subjectCode}`
          console.log({ ...param })
          this.query = { ...this.query, ...param }
        }
      },
      async success() {
        try {
          var flag = await this.$refs.form.validate()
          if (flag) {
            const params = {
              ...this.query
            }
            const res = await accountSubjectSave(params)
            if (res.success) {
              this.reset()
              this.$emit('refresh')
            }
          }
        } catch (e) {
          1
        }
      },
      reset() {
        this.query = {
          subjectCode: null,
          showIndex: null,
          remark: null,
          subjectName: null,
          subjectFlag: null
        }
        this.visible = false
      }
    }

  }
</script>

<style lang="scss" scoped>

</style>

